<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>BunnyWise - Free Writing</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='write_mobile.css') }}">
    <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
    <script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
    <style>
        /* 自定义 Quill 编辑器样式 */
        .ql-container {
            height: 300px;
            font-size: 16px;
            border-radius: 8px;
        }
        .ql-editor {
            padding: 15px;
        }
        .ql-toolbar {
            display: none;  /* 隐藏工具栏 */
        }
        .ai-completion {
            color: #888;
        }
    </style>
</head>
<body>
    <div class="logo-container">
        <img src="{{ url_for('static', filename='bunnyspell.png') }}" alt="BunnyWise Logo" class="logo">
    </div>
    
    <div class="mobile-writing-container">
        <h1 class="mobile-writing-title">Free Writing</h1>
        <div class="mobile-input-wrapper">
            <div id="editor"></div>
            <div id="loading-spinner" class="mobile-loading-spinner"></div>
        </div>
    </div>

    <script>
    document.addEventListener('DOMContentLoaded', function() {
        let typingTimer;
        const doneTypingInterval = 2000;
        let currentCompletion = '';
        
        // 初始化 Quill
        const quill = new Quill('#editor', {
            theme: 'snow',
            placeholder: 'Start writing in English...',
            formats: ['color']  // 只允许颜色格式
        });

        quill.on('text-change', function() {
            if (!quill.hasFocus()) return;
            
            clearTimeout(typingTimer);
            const text = quill.getText();
            const words = text.trim().split(/\s+/);
            
            if (words.length >= 5) {
                typingTimer = setTimeout(getCompletion, doneTypingInterval);
            }
        });

        document.addEventListener('keydown', function(e) {
            if (e.key === 'Tab' && currentCompletion) {
                e.preventDefault();
                const delta = quill.getContents();
                // 将灰色文本转换为正常颜色
                quill.formatText(delta.length() - currentCompletion.length, currentCompletion.length, {
                    'color': '#000000'
                });
                currentCompletion = '';
            }
        });

        async function getCompletion() {
            const loadingSpinner = document.getElementById('loading-spinner');
            loadingSpinner.style.display = 'block';
            
            try {
                const text = quill.getText();
                const response = await fetch('/api/get-completion', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ text: text })
                });
                const data = await response.json();
                currentCompletion = ' ' + data.completion;
                
                // 插入灰色补全文本
                const position = quill.getLength() - 1;
                quill.insertText(position, currentCompletion, {
                    'color': '#888888'
                });
                
                loadingSpinner.style.display = 'none';
            } catch (error) {
                console.error('Error getting completion:', error);
                loadingSpinner.style.display = 'none';
            }
        }
    });
    </script>
        <footer class="site-footer">
            <p>Copyright &copy; {{ current_year }} 深圳喜乐时代科技 粤ICP备2024344816号</p>
        </footer>

</body>
</html>
